package com.zmn.brs.dubbo.impl.objective;

import com.alibaba.fastjson.JSONObject;
import com.zmn.brs.business.interfaces.objective.ObjectiveWarnBService;
import com.zmn.brs.common.model.dio.objective.ObjectiveWarnDIO;
import com.zmn.brs.common.model.dio.objective.ObjectiveWarnQueryDIO;
import com.zmn.brs.dubbo.constant.DubboConsts;
import com.zmn.brs.dubbo.interfaces.objective.ObjectiveWarnListRemoteService;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
import com.zmn.mcc.common.dto.staff.StaffDRO;
import com.zmn.mcc.dubbo.interfaces.staff.StaffListRemoteService;
import com.zmn.okr.common.constant.ObjectiveMasterTypeConst;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import javax.annotation.Resource;
import java.util.*;

/**
 * 类描述：目标指标预警 远程接口实现
 * @author chongw
 * @since 2021/06/09 13:41
 */
@Slf4j
@Service(version = DubboConsts.INTERFACE_VERSION, validation = "true")
public class ObjectiveWarnListRemoteServiceImpl implements ObjectiveWarnListRemoteService {

    public static final String TAG = "目标指标预警远程接口实现";

    @Reference(version = com.zmn.mcc.dubbo.dto.DubboConsts.INTERFACE_VERSION, check = false)
    private StaffListRemoteService staffListRemoteService;

    @Resource
    private ObjectiveWarnBService objectiveWarnBService;

    /**
     * 描述: 目标监控指标查询
     * @param query
     * 目标监控指标查询
     * @return com.zmn.common.dto.ResponseDTO<java.lang.Boolean>
     * @author wangjie
     * @since 2019/8/2
     **/
    @Override
    @ReadOnlyConnection
    public ResponseDTO<ObjectiveWarnDIO> listObjectiveWarnByDIO(ObjectiveWarnQueryDIO query){
        try {
            if (query.getObjMasterType() == null) {
                return ResponseDTO.fail("目标考核对象类型不能为空");
            }

            if(!ObjectiveMasterTypeConst.MASTERTYPE_DEPT.equals(query.getObjMasterType())
                && !ObjectiveMasterTypeConst.MASTERTYPE_DEFAULT.equals(query.getObjMasterType())
                && !ObjectiveMasterTypeConst.MASTERTYPE_PERSON.equals(query.getObjMasterType()) ){
                return ResponseDTO.fail("目标考核对象类型错误");
            }

            if (NumberUtil.isNullOrZero(query.getObjMasterId())) {
                return ResponseDTO.fail("目标考核对象ID不能为空");
            }

            if (NumberUtil.isNullOrZero(query.getSceneId())) {
                return ResponseDTO.fail("场景ID不能为空");
            }

            if (NumberUtil.isNullOrZero(query.getJobType())) {
                return ResponseDTO.fail("任务类型不能为空");
            }

            if(StringUtil.isBlank(query.getTaskVersionId())){
                return ResponseDTO.fail("任务版本号不能为空");
            }

            ObjectiveWarnDIO objectiveWarnDIO = objectiveWarnBService.getObjectiveWarnByQuery(query);
            try {
                ResponseDTO<StaffDRO> responseDTO = staffListRemoteService.getStaff(query.getObjMasterId());
                if(responseDTO.isSuccess()){
                    StaffDRO staffDRO = Optional.ofNullable(responseDTO.getData()).orElse(new StaffDRO());
                    objectiveWarnDIO.setStaffId(staffDRO.getStaffId());
                    objectiveWarnDIO.setStaffName(staffDRO.getRealName());
                }
            }catch (Exception e){
                log.error("[{}], 获取员工信息staffListRemoteService#getStaff，员工ID：{}，异常信息：{}" , TAG , query.getObjMasterId() , e);
            }
            log.info("[{}], 接口入参：{}, 数据获取成功！" , TAG , JSONObject.toJSONString(query));
            return ResponseDTO.success(objectiveWarnDIO);
        }catch (Exception e){
            log.error("[{}], 接口入参：{}, 异常信息：{}" , TAG , JSONObject.toJSONString(query) , e);
            return ResponseDTO.fail("获取监控数据出错");
        }
    }
}
